// pages/index/productDetails/productDetails.js

import {
  // 查询商品xiangq
  shopGoodsInfo,
  //新增评论
  commentAdd,
  //修改评论
  commentUpdate,
  //删除评论
  commentDelete,
  //添加购物车
  addCart,
  //获取购物车
  getCart
} from '../../../api/index'

import {
  formatDate
} from '../../../utils/util'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //商品数据
    comObj: {},
    // 评论数据
    commentList: [],
    // 自己的评论信息
    myComment: {},
    // 自己是否评论
    isComment: false,
    // 评论信息
    searchValue: '',
    // 时间处理
    formatDate,
    //是否是发布评论
    type: true,
    typeText: '发布',
    commentId: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let comObj = options.obj
    if (comObj) {
      comObj = JSON.parse(comObj)
      console.log("商品数据：", comObj);
      // 获取最新商品数据
      this.getGoodsInfo(comObj.id)
    }

  },

  // 商品详情查询
  async getGoodsInfo(id) {
    const dataObj = await shopGoodsInfo({
      id
    })

    console.log("订单查询结果：", dataObj);
    if (dataObj.code == 200) {
      // dataObj.data.forEach(item => {
      //   item.create_time = formatDate(item.create_time)
      // })
      const cartObj = await getCart()
      console.log("购物车：", cartObj);
      let goodsObj = {}
      if (cartObj.code == 200) {
        goodsObj = cartObj.data.find(item => item.goods_id == dataObj.data.goods_info.id)
        goodsObj = goodsObj ? goodsObj : {}
      }
      console.log(goodsObj);
      this.setData({
        comObj: dataObj.data.goods_info,
        commentList: dataObj.data.comment_list,
        myComment: dataObj.data.my_comment || {},
        isComment: dataObj.data.my_comment ? true : false,
        // 购物车数量
        cartNum: goodsObj.goods_count ? goodsObj.goods_count : 0
      })
    }
  },

  // 发布评论
  async onAddComments() {
    console.log("发布内容", this.data.searchValue);
    const content = this.data.searchValue
    const goods_id = this.data.comObj.id
    try {
      if (this.data.type) {
        const dataObj = await commentAdd({
          goods_id,
          // 星级
          star: 5,
          content
        })
        console.log("新增评论", dataObj);
        if (dataObj.code == 200) {
          this.getGoodsInfo(goods_id)
        }
        this.setData({
          searchValue: '',
        })
      } else {
        const commentId = this.data.commentId
        const dataObj = await commentUpdate({
          id: commentId,
          goods_id,
          // 星级
          star: 5,
          content
        })
        console.log("修改评论：", dataObj);
        if (dataObj.code == 200) {
          this.getGoodsInfo(goods_id)
        }
        this.setData({
          searchValue: '',
          type: true,
          typeText: '发布'
        })
      }

    } catch (error) {
      console.log("发布评论错误", error);
    }

  },

  // 修改评论
  async updateComment(e) {
    console.log(e.currentTarget.dataset.text);
    console.log("发布内容", this.data.searchValue);
    this.setData({
      searchValue: e.currentTarget.dataset.text,
      type: false,
      typeText: '修改',
      commentId: e.currentTarget.dataset.id,
      isComment: false
    })
  },

  // 删除评论
  async dleComment(e) {
    const goods_id = this.data.comObj.id
    try {
      const dataObj = await commentDelete({
        id: e.currentTarget.dataset.id
      })
      console.log("删除评论", dataObj);
      if (dataObj.code == 200) {
        this.getGoodsInfo(goods_id)
      }
      this.setData({
        searchValue: '',
        type: true,
        typeText: '发布'
      })
    } catch (error) {
      console.log("删除评论错误", error);
    }

  },

  // 添加购物车
  async addCartPage() {
    try {
      const goods_id = this.data.comObj.id
      const goods_count = this.data.cartNum + 1
      const dataObj = await addCart({
        goods_id,
        goods_count
      })
      console.log("添加购物车:", dataObj);
      if (dataObj.code == 200) {
        this.getGoodsInfo(goods_id)
      }
    } catch (error) {
      console.log("添加购物车错误", error);
    }
  },
// 前往购物车
toCart(){
wx.switchTab({
  url: '/pages/shoppingCart/shoppingCart',
})
}
})